<?php


namespace app\common\validate;


use app\common\enum\MainEnum;

class CouponValidate extends CommonValidate
{
    protected $rule    = [
        'user_id|用户ID'                        => 'require|integer|between:1,100000000',
        'coupon_id|优惠券ID'                     => 'require|integer|between:1,100000000',
        'coupon_name|优惠券名称'                   => 'require|length:1,50',
        'coupon_exchange_integral|兑换优惠券消耗的积分' => 'require|float',
        'coupon_use_min_price|最低消费多少金额可用优惠券'  => 'require|float',
        'coupon_price|最低消费多少金额可用优惠券'          => 'require|float',
        'coupon_type_manjian_price|满减的减'      => 'float',
        'coupon_type_discount_price|折扣度'      => 'integer',
        'coupon_type_random_min_price|随机最小值'  => 'float',
        'coupon_type_random_max_price|随机最大值'  => 'float',
        'coupon_valid_time|生效时长'              => 'integer',
        'coupon_product_ids|使用的商品ids'         => 'enumIds',
        'coupon_issue_status|优惠券发放状态'         => 'require|bool',
        'coupon_issue_time|优惠券发放时间段'          => 'enumYmdHis',
        'coupon_issue_total_count|优惠券发放总量'    => 'integer',
        'coupon_issue_single_limit|单人最多领取数量'  => 'integer',
        'coupon_all_product|'                 => 'require|bool',
        'user_coupon_id|用户优惠券ID'              => 'require|integer|between:1,100000000',
        'user_coupon_status|用户优惠券状态'          => 'require|enumUserCouponStatus',
        //        'user_coupon_use_time|用户优惠券使用时间'            => 'require|enumYmdHis',
        'user_coupon_record_id|用户优惠券记录ID'     => 'require|integer|between:1,100000000',
        'user_coupon_get_type|用户优惠券获取方式'      => 'require|enumUserCouponGetType',
        'coupon_object_type|发放对象'             => 'require|integer',
    ];
    protected $message = [];
    protected $scene   = [
        'postCoupon'          => [
            'coupon_name',
            'coupon_use_min_price',
            'coupon_type',
            'coupon_type_jian_price',
            'coupon_type_discount_price',
            'coupon_type_random_min_price',
            'coupon_type_random_max_price',
            'coupon_valid_time',
            'coupon_product_ids',
            'coupon_exchange_integral',
            'coupon_all_product',
            'coupon_issue_status',
            'coupon_issue_time',
            'coupon_issue_total_count',
            'coupon_issue_single_limit',
        ],
        'putCoupon'           => [
            'coupon_id',
            'coupon_name',
            'coupon_use_min_price',
            'coupon_type',
            'coupon_type_jian_price',
            'coupon_type_discount_price',
            'coupon_type_random_min_price',
            'coupon_type_random_max_price',
            'coupon_valid_time',
            'coupon_product_ids',
            'coupon_exchange_integral',
            'coupon_all_product',
            'coupon_issue_status',
            'coupon_issue_time',
            'coupon_issue_total_count',
            'coupon_issue_single_limit',
        ],
        'deleteCoupon'        => ['coupon_id'],
        'postCouponAdminGive' => [
            'coupon_id',
            'coupon_object_type',
        ],
        #user领取
        'postCouponIssue'     => [
            'coupon_id',
        ],
    ];

    protected function enumUserCouponStatus($value): bool
    {
        try {
            $status = array_values(MainEnum::COUPON_USER_STATUS);
            if (in_array($value, $status, true)) {
                return true;
            }
            return false;
        } catch (\Exception $e) {
            return false;
        }
    }

    protected function enumUserCouponGetType($value): bool
    {
        try {
            $values = array_values(MainEnum::COUPON_USER_GET_TYPE);
            if (in_array($value, $values, true)) {
                return true;
            }
            return false;
        } catch (\Exception $e) {
            return false;
        }
    }
}